/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2014 All Rights Reserved.
 */
package com.h2o3.sample.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.h2o3.carmack.message.MessageFactory;
import com.h2o3.carmack.session.Session;
import com.h2o3.carmack.session.SessionGroup;
import com.h2o3.sample.service.ChatService;

/**
 * @author chensheng
 * @version $Id: ChatServiceImpl.java, v 0.1 1/22/14 6:33 PM chensheng Exp $
 */
@Service
public class ChatServiceImpl implements ChatService {
    private SessionGroup   sessions = new SessionGroup();

    @Autowired
    private MessageFactory messageFactory;

    @Override
    public void join(Session session) {
        sessions.add(session);
    }

    @Override
    public void quit(Session session) {
        sessions.remove(session);
    }

    @Override
    public void sendMessage(Session session, String message) {
        session.broadcast(sessions,
            messageFactory.createMessage("message", session.getSessionKey() + " say: " + message));
    }

    public MessageFactory getMessageFactory() {
        return messageFactory;
    }

    public void setMessageFactory(MessageFactory messageFactory) {
        this.messageFactory = messageFactory;
    }
}
